/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.unpack.file.utils;

import cn.ac.iie.unpack.file.global.GlobalParas;
import cn.ac.iie.unpack.file.main.UnpackFileMain;
import static cn.ac.iie.unpack.file.utils.FileUntarAndMoveThread.LOG;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author moli
 */
public class FileListGetThread implements Callable<Integer> {

    Logger LOG = Logger.getLogger(FileListGetThread.class);
    public volatile static boolean Stop;
    @Override
    public Integer call() {
        File tarFile = new File(GlobalParas.SOURCE_DIR);
        if (!tarFile.exists()) {
            LOG.error(String.format("%s dir does not exists.", tarFile));
        }
        while (!UnpackFileMain.Stop) {
            LOG.info("try to get file lists from dir "+GlobalParas.SOURCE_DIR);
            String[] fileLists = tarFile.list();
            List<String> fileLists1 = Arrays.asList(fileLists);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                LOG.error(ExceptionUtils.getFullStackTrace(ex));
            }
            for (String file : fileLists) {
                try {
                    if (!file.endsWith(GlobalParas.FILE_SUFFIX) && !fileLists1.contains(file + GlobalParas.FILE_SUFFIX)) {
                        new File(GlobalParas.SOURCE_DIR+File.separator+file + GlobalParas.FILE_SUFFIX).createNewFile();
                        GlobalParas.FILE_QUEUE.put(GlobalParas.SOURCE_DIR+File.separator+file);
                        LOG.info("put 1 file to queue, file name: " + file);
                    }
                } catch (InterruptedException ex) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex));
                } catch (IOException ex1) {
                    LOG.error(ExceptionUtils.getFullStackTrace(ex1));
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                LOG.error(ExceptionUtils.getFullStackTrace(ex));
            }
        }
        return 1;

    }

}
